from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.enum.chart import XL_LEGEND_POSITION
from pptx.util import Pt

# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# define chart data ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

# add chart to slide --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels

data_labels.position = XL_LABEL_POSITION.INSIDE_END

chart.has_title = True
chart.chart_title.text_frame.clear()  # 清除原标题
new_paragraph = chart.chart_title.text_frame.add_paragraph()  # 添加一行新标题
new_paragraph.text = 'B班级选项占比'  # 新标题
new_paragraph.font.size = Pt(13)  # 新标题字体大小

f = open('chart-01.pptx', 'wb')
prs.save(f)
f.close()